package com.gsx.service.Impl;

import com.github.pagehelper.PageHelper;
import com.gsx.entity.Blog_Type_User;
import com.gsx.entity.Tag;
import com.gsx.mapper.TagMapper;
import com.gsx.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;

    @Transactional
    @Override
    public int saveTag(Tag tag) {
        return tagMapper.saveTag(tag);
    }

    @Override
    public Tag getTag(Long id) {
        return tagMapper.getTag(id);
    }

    @Override
    public List<Tag> getAllTag(int page, int size) {
        PageHelper.startPage(page,size);
        return tagMapper.getListTag();
    }

    @Transactional
    @Override
    public int updateTag(Tag tag) {
        return tagMapper.updateTag(tag);
    }

    /*封装成事务  出错会回滚*/
    @Transactional
    @Override
    public int deleteTag(Long id) {
        return tagMapper.deleteTag(id);
    }

    public List<Tag> getAll(){
        return tagMapper.getAll();
    }

    @Transactional
    public Tag getTagByName(String name){
        return tagMapper.getTagByName(name);
    }

    public int bt_idInsert(Long blog_id,String name){
        int count=0;
        String[] str=name.split(",");
        Long temp;
        for(String s:str){
            count++;
            temp=Long.parseLong(s);
            tagMapper.bt_idInsert(blog_id,temp);
        }
        return count;
    }

    public int delBlog_tag(Long blog_id){
        return tagMapper.delBlog_tag(blog_id);
    }

    public List<Tag> getBlog_tag(Long blog_id){
        return tagMapper.getBlog_tag(blog_id);
    }

    public List<Blog_Type_User> getBlog_Tags(Long tag_id){
        return tagMapper.getBlog_Tags(tag_id);
    }
}
